package com.yimi.yinhepda.db.dao;

import com.j256.ormlite.stmt.QueryBuilder;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.BreakCardTable;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 打卡数据操作
 * @date 2018/6/9 0009
 */

public class BreakCardTableDao extends BaseDao<BreakCardTable, Long> {

    public BreakCardTableDao() {
        super(DbHelper.getInstance(BaseApplication.getContext()), BreakCardTable.class);
    }

    public BreakCardTable getDate(String stowageNo, String workNum, int clockType, String deptCode) {
        try {
            QueryBuilder<BreakCardTable, Long> queryBuilder = dao.queryBuilder();
            BreakCardTable breakCardTable = queryBuilder.where()
                    .eq("stowageNo", stowageNo)
                    .and()
                    .eq("puncherCode", workNum)
                    .and()
                    .eq("clockType", clockType)
                    .and()
                    .eq("deptCode", deptCode)
                    .queryForFirst();
            return breakCardTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询班次数据
     */
    public BreakCardTable getDateByShiftOrder(String clockDate, int shiftsOrder, String workNum, int clockType, String deptCode, String relatDeptCode) {
        try {
            QueryBuilder<BreakCardTable, Long> queryBuilder = dao.queryBuilder();
            BreakCardTable breakCardTable = queryBuilder.where()
                    .eq("clockDate", clockDate)
                    .and()
                    .eq("shiftsOrder", shiftsOrder)
                    .and()
                    .eq("puncherCode", workNum)
                    .and()
                    .eq("clockType", clockType)
                    .and()
                    .eq("deptCode", deptCode)
                    .and()
                    .eq("relatDeptCode", relatDeptCode)
                    .queryForFirst();
            return breakCardTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<BreakCardTable> getDates(String workNum, int clockType, String deptCode) {
        try {
            if (getDbHelper() != null && getDbHelper().isOpen()) {
                QueryBuilder<BreakCardTable, Long> queryBuilder = dao.queryBuilder();
                List<BreakCardTable> lists =
                        queryBuilder.where().eq("puncherCode", workNum).and().eq("clockType", clockType).and().eq("deptCode", deptCode).query();
                return lists;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
